package com.hencoder.hencoderpracticedraw1.learn

import android.content.Context
import android.graphics.*
import android.os.Build
import android.support.annotation.RequiresApi
import android.util.AttributeSet
import android.view.View


class DrowOftenView : View {


    constructor(context: Context) : super(context)

    constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)

    constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int) : super(
        context,
        attributeSet,
        defStyleAttr
    )

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
//        drawCircle(canvas!!)
//        drawRect(canvas)
//        drawPoint(canvas)
//        drawOval(canvas!!)
//        drawLine(canvas!!)
//        drawRoundRect(canvas!!)
        drawArc(canvas!!)
    }

    //画圆
    fun drawCircle(canvas: Canvas) {
        val paint = Paint()
        paint.color = Color.RED
        paint.isAntiAlias = true   //抗锯齿
        canvas.drawCircle(300f, 300f, 200f, paint)
    }

    //画矩形
    fun drawRect(canvas: Canvas) {
        val paint = Paint()    // 画笔
//        paint.style=Paint.Style.FILL  //FILL填充
        paint.style = Paint.Style.STROKE      //画线
        paint.strokeWidth = 20f               //线的狂赌
        paint.color = Color.BLUE      //设置颜色
        canvas.drawRect(100f, 200f, 300f, 100f, paint)
    }

    //画点
    fun drawPoint(canvas: Canvas) {
        val points = floatArrayOf(
            0f,
            0f,
            50f,
            50f,
            100f,
            100f,
            200f,
            200f,
            300f,
            300f,
            400f,
            400f,
            500f,
            500f,
            600f,
            600f
        )
        val paint = Paint()    // 画笔
        paint.color = Color.GRAY
        paint.strokeWidth = 30f   //点的大小
        paint.strokeCap = Paint.Cap.BUTT   //ROUND 画出来是圆形的点  SQUARE 或 BUTT 画出来是方形的点
//        canvas.drawPoint(300f, 300f, paint)
        canvas.drawPoints(points, paint)             //画一堆点
//        canvas.drawPoints(points, 2 , 8 , paint)/* 跳过两个数，即前两个 0 一共绘制 8 个数（4 个点）*/
    }


    //画椭圆
    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    fun drawOval(canvas: Canvas) {
        val paint = Paint()
        paint.color = Color.RED
        //left, top, right, bottom 是这个椭圆的左、上、右、下四个边界点的坐标。
        canvas.drawOval(200f, 200f, 401f, 1500f, paint)
    }

    //画线
    fun drawLine(canvas: Canvas) {
        val paint = Paint()
        paint.color = Color.RED
        canvas.drawLine(100f, 200f, 500f, 500f, paint)
    }


    //画圆角矩形
    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    fun drawRoundRect(canvas: Canvas) {
        val paint = Paint()
        paint.color = Color.RED
//        left, top, right, bottom 是四条边的坐标，rx 和 ry 是圆角的横向半径和纵向半径。
        canvas.drawRoundRect(200f, 400f, 600f, 800f, 20f, 20f, paint)
    }

    //画扇形
    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    fun drawArc(canvas: Canvas) {
        val rectF = RectF(200f, 200f, 400f, 400f)

        val paint = Paint()
        paint.color = Color.RED
        paint.style = Paint.Style.FILL
//        drawArc() 是使用一个椭圆来描述弧形的。left, top, right, bottom 描述的是这个弧形所在的椭圆；
//        startAngle 是弧形的起始角度（x 轴的正向，即正右的方向，是 0 度的位置；顺时针为正角度，逆时针为负角度），
//        sweepAngle 是弧形划过的角度；useCenter 表示是否连接到圆心，如果不连接到圆心，就是弧形，如果连接到圆心，就是扇形。
        canvas.drawArc(rectF, 0f, 30f, true, paint)

        val rectB = RectF(170f, 170f, 430f, 430f)
        paint.color = Color.BLUE
        canvas.drawArc(rectB, 90f, 120f, true, paint)

        paint.color = Color.GREEN
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 5f
        canvas.drawArc(rectF, 30f, 50f, true, paint)

        paint.color = Color.GREEN
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 10f
        canvas.drawArc(rectF, 220f, 150f, false, paint)
    }




}